{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import datetime\n",
    "import pyspark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = pyspark.SparkContext()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [chr(random.randint(65,91)) for i in range(1000000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "rdd = sc.parallelize(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成一个reduceByKey之后的rdd2，连续两次进行提交"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可以看见，第二次稍微快点，是因为第二次不用进行任务调度，节约了任务调度的时间，但是依然耗费了计算时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:00:10.000775\n",
      "-------------\n",
      "0:00:05.434287\n",
      "-------------\n",
      "0:00:15.435062\n",
      "#############\n",
      "[('N', 36919), ('[', 37345), ('O', 37158), ('J', 37141), ('H', 36538), ('B', 37152), ('R', 36929), ('C', 37090), ('W', 36951), ('Z', 37120), ('I', 36969), ('V', 36720), ('X', 37498), ('L', 36923), ('K', 37071), ('Q', 37015), ('A', 37038), ('M', 37333), ('T', 36822), ('U', 36748), ('F', 37038), ('S', 36858), ('E', 36917), ('Y', 37405), ('G', 37114), ('P', 37035), ('D', 37153)]\n",
      "[('N', 36919), ('[', 37345), ('O', 37158), ('J', 37141), ('H', 36538), ('B', 37152), ('R', 36929), ('C', 37090), ('W', 36951), ('Z', 37120), ('I', 36969), ('V', 36720), ('X', 37498), ('L', 36923), ('K', 37071), ('Q', 37015), ('A', 37038), ('M', 37333), ('T', 36822), ('U', 36748), ('F', 37038), ('S', 36858), ('E', 36917), ('Y', 37405), ('G', 37114), ('P', 37035), ('D', 37153)]\n"
     ]
    }
   ],
   "source": [
    "rdd2 = rdd.map(lambda a : (a,1)).reduceByKey(lambda a,b : a+b)\n",
    "s = datetime.datetime.now()\n",
    "res1 = rdd2.collect()\n",
    "rt1 = datetime.datetime.now() -s \n",
    "print(rt1)\n",
    "print(\"-------------\")\n",
    "s = datetime.datetime.now()\n",
    "res2 = rdd2.collect()\n",
    "rt2 = datetime.datetime.now() -s \n",
    "print(rt2)\n",
    "print(\"-------------\")\n",
    "print(rt1 + rt2)\n",
    "print(\"#############\")\n",
    "print(res1)\n",
    "print(res2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 直接在reduceByKey的时候，生成一个缓存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第二次执行，就只需要从缓存RDD来执行，仅需要1秒钟。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:00:10.037251\n",
      "-------------\n",
      "0:00:01.072305\n",
      "-------------\n",
      "0:00:11.109556\n",
      "#############\n",
      "[('N', 36919), ('[', 37345), ('O', 37158), ('J', 37141), ('H', 36538), ('B', 37152), ('R', 36929), ('C', 37090), ('W', 36951), ('Z', 37120), ('I', 36969), ('V', 36720), ('X', 37498), ('L', 36923), ('K', 37071), ('Q', 37015), ('A', 37038), ('M', 37333), ('T', 36822), ('U', 36748), ('F', 37038), ('S', 36858), ('E', 36917), ('Y', 37405), ('G', 37114), ('P', 37035), ('D', 37153)]\n",
      "[('N', 36919), ('[', 37345), ('O', 37158), ('J', 37141), ('H', 36538), ('B', 37152), ('R', 36929), ('C', 37090), ('W', 36951), ('Z', 37120), ('I', 36969), ('V', 36720), ('X', 37498), ('L', 36923), ('K', 37071), ('Q', 37015), ('A', 37038), ('M', 37333), ('T', 36822), ('U', 36748), ('F', 37038), ('S', 36858), ('E', 36917), ('Y', 37405), ('G', 37114), ('P', 37035), ('D', 37153)]\n"
     ]
    }
   ],
   "source": [
    "rdd2 = rdd.map(lambda a : (a,1)).reduceByKey(lambda a,b : a+b).cache()\n",
    "s = datetime.datetime.now()\n",
    "res = rdd2.collect()\n",
    "rt1 = datetime.datetime.now() -s \n",
    "print(rt1)\n",
    "print(\"-------------\")\n",
    "s = datetime.datetime.now()\n",
    "res2 = rdd2.collect()\n",
    "rt2 = datetime.datetime.now() -s \n",
    "print(rt2)\n",
    "print(\"-------------\")\n",
    "print(rt1 + rt2)\n",
    "print(\"#############\")\n",
    "print(res1)\n",
    "print(res2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
